const app = getApp();
import grace from "../../../utils/grace.js";

grace.page({
  /**
   * 页面的初始数据
   */
  data: {
    banners: [],
    posters: [],
    total: 0,
    page: 1,
    pagesize: 10,
    role_guardian: 'yes',// 家长
    role_master: 'no',// 班主任
    role_manager: 'no',// 管理者
    role_logistics: 'no',// 后勤人员
    role_security: 'no'// 安保人员
  },
  onLoad(e) {

  },
  onShow() {
    let that = this;

    // 如果未设置幼儿园，则跳转选择幼儿园页面
    if(wx.getStorageSync('school_id') == ''){
      wx.navigateTo({
        url: '/pages/home/index/setting'
      })
      return
    }

    let token = wx.getStorageSync('token');

    // 加载最新帖子
    this.getList();

    wx.showLoading({
      title: '加载中...',
    })

    let school_id = wx.getStorageSync('school_id')
    // 加载banner
    let url = app.globalData.api + '/api/v1.0/banners/' + school_id + '/home_index';
    that.$http.get(url).then((res) => {
      if (res.data.code == 0) {
        that.$data.banners = res.data.data;
      }
    }).catch(err => {
      console.log(err.status, err.message);
    })

    // 获取最新的role
    url = app.globalData.api + '/api/v1.0/wechatUserRoleAPI/' + school_id;
    that.$http.get(url, {token: token}).then((res) => {
      if (res.data.code == 0) {
        if (res.data.data.role_guardian == 'yes') {
          wx.setStorageSync('role_guardian', 'yes');
          that.$data.role_guardian = 'yes';
        }
        if (res.data.data.role_master == 'yes') {
          wx.setStorageSync('role_master', 'yes');
          that.$data.role_master = 'yes';
        }
        if (res.data.data.role_manager == 'yes') {
          wx.setStorageSync('role_manager', 'yes');
          that.$data.role_manager = 'yes';
        }
        if (res.data.data.role_logistics == 'yes') {
          wx.setStorageSync('role_logistics', 'yes');
          that.$data.role_logistics = 'yes';
        }
        if (res.data.data.role_security == 'yes') {
          wx.setStorageSync('role_security', 'yes');
          that.$data.role_security = 'yes';
        }
        wx.setStorageSync('school_name', res.data.data.school_name);
        wx.setStorageSync('class_id', res.data.data.class_id);
        wx.setStorageSync('class_name', res.data.data.class_name);
        wx.setStorageSync('user_code', res.data.data.user_code);
      }else{
        console.log('获取权限异常');
      }
      wx.hideLoading();
    }).catch(err => {
      console.log(err.status, err.message);
    })
  },
  go_member() {
    wx.switchTab({
      url: '/pages/common/center/index'
    })
  },
  getList() {
    let that = this;
    let school_id = wx.getStorageSync('school_id')
    wx.showLoading({
      title: '加载中...',
    })
    let url = app.globalData.api + '/api/v1.0/posterList/' + school_id + '/0' + '?page=' + that.$data.page + '&pagesize=' + that.$data.pagesize;
    that.$http.get(url).then((res) => {
      if (res.data.code == 0) {
        that.$data.total = res.data.data.total;
        that.$data.page = that.$data.page + 1;
        that.$data.posters = that.$data.posters.concat(res.data.data.list);
      }
      wx.hideLoading();
    }).catch(err => {
      console.log(err.status, err.message);
    });
  },
  previewImage: function (e) {
    let imageUrls = [];
    for (let i = 0; i < e.currentTarget.dataset.images.length; i++) {
      imageUrls.push(e.currentTarget.dataset.images[i].pic_url);
    }
    wx.previewImage({
      current: imageUrls[0].pic_url,
      urls: imageUrls
    })
  }
})